
import GameManager from "../game/GameManager";
import Constants from "../libs/Constants";
import GameData from "../libs/GameData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class UIOver extends cc.Component {


    @property(cc.Node)
    win: cc.Node = null;

    @property(cc.Node)
    fail: cc.Node = null;

    @property(cc.Node)
    normal: cc.Node = null;
   
    rewardNum:number = 0;

    start () {
        this.rewardNum = Math.round(GameData.instance.playScore/10);
        this.win.active = false;
        this.fail.active = false;
        this.normal.active = false;
        if(GameData.instance.gameModel){
            if(GameData.instance.playScore >= Constants.StageInfo[GameData.instance.gameStage-1].winscore){
                this.refreshWinNode()
            }else{
                this.refreshFailNode();
            }
        }else{
            this.refreshNormalNode();
        }
        
    }


    refreshWinNode(){
        this.win.active = true;
        GameData.instance.gameStage ++;
        if(GameData.instance.gameStage > Constants.StageInfo.length){
            GameData.instance.gameStage = 1;
        }
        let num1 = this.win.getChildByName("Num").getComponent(cc.Label);
        let num2 = this.win.getChildByName("SignleGet").getChildByName("num").getComponent(cc.Label);
        num1.string = this.rewardNum.toString();
        num2.string = this.rewardNum.toString();

    }

    refreshFailNode(){
        this.fail.active = true;
        let num1 = this.fail.getChildByName("Num").getComponent(cc.Label);
        let num2 = this.fail.getChildByName("SignleGet").getChildByName("num").getComponent(cc.Label);
        num1.string = this.rewardNum.toString();
        num2.string = this.rewardNum.toString();

    }


    refreshNormalNode(){
        this.normal.active = true;

    }


    onGet(event,customEventData){
        let para = parseInt(customEventData);
        GameData.instance.playGold += this.rewardNum*para;
        this.recordScore();
        GameManager.instace.backMain();
      
    }

    onSure(): void {
        this.recordScore();
        GameManager.instace.backMain();
        
    }



   

    recordScore(){
        if(GameData.instance.gameModel){
            GameData.instance.playScores.push(GameData.instance.playScore)
            GameData.instance.playScores.sort((a,b)=>{return b - a})
        }else{
            GameData.instance.playCrazyScores.push(GameData.instance.playScore)
            GameData.instance.playCrazyScores.sort((a,b)=>{return b - a})

        }
       
        GameData.instance.saveData();
    }

}
